#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>
#include "readwave.h"
#include "SpectralFeat.h"

void main(int argc, char **argv)
{

#ifdef _DEBUG
	_CrtSetBreakAlloc(0);
#endif

	float SP_Cent = 0;
	float SP_spread = 0;
	float SP_skew = 0;
	float SP_kurt = 0;
	float SP_slope = 0;
	float SP_decr = 0;
	float SP_rolloff = 0;

	float SP_SFM = 0;
	float SP_SCM = 0;
	float SP_Tonality = 0;

	char filename[256];
	strcpy(filename, argv[1]);

	int numSamples, nSmpRate;
	WavHead wavhead;
	short *WaveData = ReadWave(filename, &numSamples, &wavhead);
	nSmpRate = wavhead.SampleRate;

	SPECTRAL_FEATBASEINFO sp_baseinfo;	
	SpectralFeat *spectFeat = new SpectralFeat;
	spectFeat->Initialize(sp_baseinfo);

	int nFrameShift = sp_baseinfo.framePeriod * sp_baseinfo.nSmpRate/1e3 ;
	int nWinSize = sp_baseinfo.winSize * sp_baseinfo.nSmpRate/1e3;
	int numFrame = (numSamples-nWinSize)/nFrameShift+1;

	FILE *fp = fopen("spectFeat.txt", "w");
	int i;
	short *pCurWave=WaveData;
	for (i=0; i<numFrame; i++)
	{
		spectFeat->AddWavedata(pCurWave,nWinSize,nSmpRate);

		spectFeat->CalculAllShapeFeat();
		spectFeat->CalculAllFlatFeat();

		SP_Cent = spectFeat->m_fCent;
		SP_spread = spectFeat->m_fSpread;
		SP_skew = spectFeat->m_fSkew;
		SP_kurt = spectFeat->m_fKurt;
		SP_slope = spectFeat->m_fSlope;
		SP_decr = spectFeat->m_fDecr;
		SP_rolloff = spectFeat->m_fRolloff;

		SP_SFM = spectFeat->m_fSFM;
		SP_SCM = spectFeat->m_fSCM;
		SP_Tonality = spectFeat->m_fTonality;

		fprintf(fp,"%.2f\t\t%.2f\t\t%.2f\t\t%.2f\t\t%.7f\t\t%.4f\t\t%.2f\t\t%.3f\t\t%.3f\t\t%.3f\n",SP_Cent, SP_spread, SP_skew, SP_kurt, SP_slope, SP_decr, SP_rolloff, SP_SFM, SP_SCM, SP_Tonality);
		pCurWave += nFrameShift;
	}

	fclose(fp);

	delete []WaveData;
	delete spectFeat;

#ifdef _DEBUG
	_CrtDumpMemoryLeaks();
#endif
}
